/*
 * Getrs.cs
 * 
 * Copyright (c) 2003-2004, dnAnalytics Project. All rights reserved.
*/
#if !MANAGED
using System;
using System.Runtime.InteropServices;

using dnA.Utility;

namespace dnA.Math.Lapack{
	[System.Security.SuppressUnmanagedCodeSecurityAttribute]
	internal sealed class Getrs {
		private  Getrs() {}
		private static void ArgumentCheck(int n, int nrhs, Object A, int lda, Object B, int ldb, int[] ipiv) {
			if ( A == null ) {
				throw new ArgumentNullException("A","A cannot be null.");
			}
			if ( B == null ) {
				throw new ArgumentNullException("B","B cannot be null.");
			}
			if ( n<0 ) {
				throw new ArgumentException("n must be at least zero.");
			}
			if ( nrhs<0 ) {
				throw new ArgumentException("nrhs must be at least zero.");
			}
			if ( lda < System.Math.Max(1,n) ) {
				throw new ArgumentException("lda must be at least max(1,m)");
			}
			if ( ldb < System.Math.Max(1,n) ) {
				throw new ArgumentException("ldb must be at least max(1,m)");
			}
			if ( ipiv.Length < System.Math.Max(1,n) ) {
				throw new ArgumentException("The length of ipiv must be at least max(1,n)");
			}
		}
	
		internal static int Compute( Transpose trans, int n, int nrhs, float[] A, int lda, int[] ipiv, float[] B, int ldb ){
			ArgumentCheck(n, nrhs, A, lda,  B, ldb, ipiv);
			if ( trans == Transpose.ConjTrans ) {
				trans = Transpose.Trans;
			}
			
			return dna_lapack_sgetrs(trans,n,nrhs,A,lda,ipiv,B,ldb);
		}

		internal static int Compute( Transpose trans, int n, int nrhs, double[] A, int lda, int[] ipiv, double[] B, int ldb ){
			ArgumentCheck(n, nrhs, A, lda,  B, ldb, ipiv);
			if ( trans == Transpose.ConjTrans ) {
				trans = Transpose.Trans;
			}
			
			return dna_lapack_dgetrs(trans,n,nrhs,A,lda,ipiv,B,ldb);
		}

		internal static int Compute( Transpose trans, int n, int nrhs, ComplexFloat[] A, int lda, int[] ipiv, ComplexFloat[] B, int ldb ){
			ArgumentCheck(n, nrhs, A, lda,  B, ldb, ipiv);
			
			return dna_lapack_cgetrs(trans,n,nrhs,A,lda,ipiv,B,ldb);
		}

		internal static int Compute( Transpose trans, int n, int nrhs, ComplexDouble[] A, int lda, int[] ipiv, ComplexDouble[] B, int ldb ){
			ArgumentCheck(n, nrhs, A, lda,  B, ldb, ipiv);
			
			return dna_lapack_zgetrs(trans,n,nrhs,A,lda,ipiv,B,ldb);
		}

		[DllImport(dnA.Utility.Configuration.BLASLibrary, ExactSpelling=true, SetLastError=false)]
		private static extern int dna_lapack_sgetrs( Transpose trans, int n, int nrhs, [In,Out]float[] A, int lda, [In]int[] ipiv, [In,Out]float[] B, int ldb );
	
		[DllImport(dnA.Utility.Configuration.BLASLibrary, ExactSpelling=true, SetLastError=false)]
		private static extern int dna_lapack_dgetrs( Transpose trans, int n, int nrhs, [In,Out]double[] A, int lda, [In]int[] ipiv, [In,Out]double[] B, int ldb );

		[DllImport(dnA.Utility.Configuration.BLASLibrary, ExactSpelling=true, SetLastError=false)]
		private static extern int dna_lapack_cgetrs( Transpose trans, int n, int nrhs, [In,Out]ComplexFloat[] A, int lda, [In]int[] ipiv, [In,Out]ComplexFloat[] B, int ldb );

		[DllImport(dnA.Utility.Configuration.BLASLibrary, ExactSpelling=true, SetLastError=false)]
		private static extern int dna_lapack_zgetrs( Transpose trans, int n, int nrhs, [In,Out]ComplexDouble[] A, int lda, [In]int[] ipiv, [In,Out]ComplexDouble[] B, int ldb );
	}
}
#endif